//
//  CLConsultDoctor.m
//  CareLink
//
//  Created by 刘家强 on 16/8/21.
//  Copyright © 2016年 Seven Team. All rights reserved.
//

#import "CLChatMeCell.h"
#import "CLConsultDoctor.h"
#import "CLChatOtherCell.h"

@interface CLConsultDoctor () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *chatArr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, weak) UIVisualEffectView *barView;
@property (nonatomic, weak) UITextField *text;

@end

@implementation CLConsultDoctor

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.navigationItem setTitle:@"咨询医生"];
    
    self.view.backgroundColor = [UIColor blackColor];

    UITableView *tableView = [[UITableView alloc] init];
    [self.view addSubview:tableView];
    self.tableView = tableView;

    tableView.delegate = self;
    tableView.dataSource = self;

    //    tableView.frame = CGRectMake(0, 200, 200, 200);

    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.bottom.offset(-44);
    }];

    [self setupUI];
}

- (void)setupUI {
    [self.chatArr addObject:@"你好,有什么可以帮助您的吗?"];
    [self.tableView registerClass:[CLChatMeCell class] forCellReuseIdentifier:@"cellMe"];
    [self.tableView registerClass:[CLChatOtherCell class] forCellReuseIdentifier:@"cellOther"];

    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 100;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    // 滑动tableView的时候 隐藏键盘
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

    // 创建一个效果
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
    // 添加一个模糊视图
    UIVisualEffectView *barView = [[UIVisualEffectView alloc] initWithEffect:effect];

    // 添加到控制器的视图
    [self.view addSubview:barView];

    // 全局接收,方便系统弹出键盘的时候 更新这个view的约束
    self.barView = barView;

    // 模糊视图的自动布局
    [barView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(44);
    }];

    // 文本框 和 按钮
    UITextField *text = [[UITextField alloc] init];
    text.borderStyle = UITextBorderStyleRoundedRect;
    // 注意:不要直接使用模糊的view添加子控件 应该是contentView
    [barView.contentView addSubview:text];

    self.text = text;

    UIButton *send = [UIButton buttonWithType:UIButtonTypeSystem];
    [send setTitle:@"发送" forState:UIControlStateNormal];
    [barView.contentView addSubview:send];
    [send addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];

    // 文本框 和 按钮 自动布局
    [text mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.offset(4);
        make.bottom.offset(-4);
    }];

    [send mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(4);
        make.right.bottom.offset(-4);
        make.left.equalTo(text.mas_right).offset(4);
        make.width.offset(44);
    }];

    // 注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)butClick:(UIButton *)sender {
    
    if ([self.text.text isEqualToString:@""]) {
        return;
    }
    
    [self.chatArr addObject:self.text.text];
    [self.tableView reloadData];
    
    self.text.text = nil;
    
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.chatArr.count - 2 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.chatArr.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.chatArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row % 2 !=  0) {
        CLChatMeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellOther" forIndexPath:indexPath];
        
        cell.chatStr = self.chatArr[indexPath.row];
        
        return cell;
    }
    
    CLChatOtherCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellMe" forIndexPath:indexPath];
    
    cell.chatStr = self.chatArr[indexPath.row];
    
    return cell;
}

#pragma mark - 懒加载
- (NSMutableArray *)chatArr {
    if (_chatArr == nil) {
        _chatArr = [NSMutableArray array];
    }
    return _chatArr;
}

// 当键盘将要隐藏的时候 会调用这个方法
- (void)keyboardWillHide:(NSNotification *)sender {
    // 更改约束 做动画

    [self.barView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(0);
    }];

    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-44);
    }];

    [UIView animateWithDuration:.2 animations:^{
        [self.tableView layoutIfNeeded];
        [self.view layoutIfNeeded];
    }];
}

// 当键盘将要弹出的时候 会调用这个方法
- (void)keyboardWillShow:(NSNotification *)sender {
    // 获取系统弹出键盘以后发送的所有的内容
    NSDictionary *dict = sender.userInfo;
    // 获取我们想要监听的键盘最后的位置
    CGRect rect = [dict[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    // 更改约束 做动画

    [self.barView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-rect.size.height);
    }];
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-rect.size.height - 44);
    }];

    [UIView animateWithDuration:.2 animations:^{
        [self.tableView layoutIfNeeded];
        [self.view layoutIfNeeded];
    }];
}

- (void)dealloc {
    // 在不需要使用监听的时候 一定一定要移除掉监听者
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
